package com.b2b.controller.company;

import com.b2b.model.SystemApiAuthorityDto;
import com.b2b.model.SystemRouteAuthorityDto;
import com.b2b.service.CompanyRouteAuthorityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.CompanyRouteAuthorityDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.util.CompanyRouteAuthorityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/companyRoute")
@PropertySource("classpath:config.properties")
public class CompanyRouteAuthorityController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final CompanyRouteAuthorityUtil companyRouteAuthorityUtil;

    private final CompanyRouteAuthorityService companyRouteAuthorityService;

    @Autowired
    public CompanyRouteAuthorityController(CompanyRouteAuthorityUtil companyRouteAuthorityUtil,
                                           CompanyRouteAuthorityService companyRouteAuthorityService){
        this.companyRouteAuthorityUtil = companyRouteAuthorityUtil;
        this.companyRouteAuthorityService = companyRouteAuthorityService;
    }

    @GetMapping("/getTree")
    public ResponseBean findTree(){
        List<CompanyRouteAuthorityDto> companyRouteAuthorityDtoList = companyRouteAuthorityService.selectAll();
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", companyRouteAuthorityDtoList);
    }
    /**
     * 获取企业路由列表
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:41
     */
    @GetMapping("/getAll")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean CompanyRouteAuthority( BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<CompanyRouteAuthorityDto> companyRouteAuthorityDtos = companyRouteAuthorityService.selectAll();
        PageInfo<CompanyRouteAuthorityDto> selectPage = new PageInfo<CompanyRouteAuthorityDto>(companyRouteAuthorityDtos);
        if (companyRouteAuthorityDtos == null || companyRouteAuthorityDtos.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }


    /**
     * 获取指定企业路由
     * @param parentId
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @GetMapping("/get")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findById(@RequestParam Integer parentId) {
        CompanyRouteAuthorityDto companyRouteAuthorityDto = new CompanyRouteAuthorityDto();
        companyRouteAuthorityDto.setParentId(parentId);
        List<CompanyRouteAuthorityDto> companyRouteAuthorityDtoList = companyRouteAuthorityService.select(companyRouteAuthorityDto);
//        if (companyRouteAuthorityDtoList.size()==0) {
//            throw new CustomException("查询失败(Query Failure)");
//        }
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", companyRouteAuthorityDtoList);
    }

    /**
     * 新增企业路由
     * @param companyRouteAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PostMapping("/add")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean add( @RequestBody CompanyRouteAuthorityDto companyRouteAuthorityDto) {
        CompanyRouteAuthorityDto companyRouteAuthorityDtoTemp = new CompanyRouteAuthorityDto();
        companyRouteAuthorityDtoTemp.setRouteName(companyRouteAuthorityDto.getRouteName());
        Integer count = companyRouteAuthorityService.selectCount(companyRouteAuthorityDtoTemp);
        if (count > 0 ) {
            throw new CustomUnauthorizedException("该企业路由已存在(Account exist.)");
        }
        count = companyRouteAuthorityService.insert(companyRouteAuthorityDto);
        if (count <= 0) {
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", companyRouteAuthorityDto);
    }


    /**
     * 更新企业路由
     * @param companyRouteAuthorityDto
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:42
     */
    @PutMapping("/update")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody CompanyRouteAuthorityDto companyRouteAuthorityDto) {
        // 查询数据库密码
        CompanyRouteAuthorityDto companyRouteAuthorityDtoTemp = new CompanyRouteAuthorityDto();
        companyRouteAuthorityDtoTemp.setRouteId(companyRouteAuthorityDto.getRouteId());
        Integer count = companyRouteAuthorityService.selectCount(companyRouteAuthorityDtoTemp);
        if (count<=0 ) {
            throw new CustomUnauthorizedException("该企业路由不存在(Account exist.)");
        }
        count = companyRouteAuthorityService.updateByPrimaryKeySelective(companyRouteAuthorityDto);
        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", companyRouteAuthorityDto);
    }

    /**
     * 删除企业路由
     * @param id
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:43
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestParam Integer id) {
        int count = companyRouteAuthorityService.deleteByPrimaryKey(id);
        if (count <= 0) {
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
}
